H5x.component("activity.activity",[],function(opts){   
    this.container =  opts.container;
    this.use("render");
    this.use("dai");
    this.use("emitter");
    this.activitydai = this.dai.create("activity");

    //this.applistdai = this.dai.create("appsetting"); 
    this.init(); 

},{
    init:function(){
        this.list_activity();
        var self = this;
        this.emitter.on("activity-refresh",function(){
            self.list_activity();
        })
    },

    list_activity:function(){
        var self = this;
        this.activitydai.query({_orderd:"no"},function(rs){
            //console.log(rs);
            self.render("tp-activity-activity",{
                list:rs.list
            });
            self.bindAction();
        });
    },

    bindAction:function(){
        var self = this;
        $(".pager-nav").make("pager");
        $("#activity-add-btn").click(function(){
            $("#activity-add-dialog .modal-dialog").make("activity.addform",{
                title:"添加活动",
                modal:$('#activity-add-dialog'),
                activity:{}
            });
            $('#activity-add-dialog').modal();
        });

        $("#activity-list").on("click",".btn_remove",function(){
            if(!confirm("你确定要删除该活动吗？")){
                return ;
            }

            var aid = $(this).parent().attr("id");
            self.activitydai.remove({id:aid},function(rs){
                //console.log(rs);
                if(rs.code>0){
                    self.list_activity();
                }
            });
        })

        $("#activity-list").on("click",".btn_edit",function(){
            var aid = $(this).parent().attr("id");
            self.activitydai.get(aid,function(rs){
                //console.log(rs);
                $("#activity-add-dialog .modal-dialog").make("activity.addform",{
                    title:"编辑活动",
                    modal:$('#activity-add-dialog'),
                    activity:rs.data
                });
                $('#activity-add-dialog').modal();
            });
        })
    }
});